/**
 * @file main.c
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2023-03-19
 *
 * @copyright Copyright (c) 2023
 *
 */
#include "bsp.h"
#include "gd32f30x.h"
#include "gd32f30x_libopt.h"
#include "systick.h"
#include <stdio.h>
#include "easyflash.h"


/**
 * @brief ENV demo
 * 
 */
static void test_env(void) {
    uint32_t i_boot_times = NULL;
    char *c_old_boot_times, c_new_boot_times[11] = {0};

    /* get the boot count number from Env */
    c_old_boot_times = ef_get_env("boot_times");
//    assert_param(c_old_boot_times);
    i_boot_times = atol(c_old_boot_times);
    /* boot count +1 */
    i_boot_times ++;
//    printf("The system now boot %d times\n\r", i_boot_times);
    /* interger to string */
    sprintf(c_new_boot_times,"%ld", i_boot_times);
    /* set and store the boot count number to Env */
    ef_set_env("boot_times", c_new_boot_times);
    ef_save_env();
}


/**
 * @brief
 *
 * @return int
 */
int main(void)
{
    uint8_t ucKeyCode;                         /* 按键代码 */
    uint32_t clk = rcu_clock_freq_get(CK_SYS); // 获取系统的运行时钟
    bsp_Init();                                /* 硬件初始化 */
    if (easyflash_init() == EF_NO_ERR)
    {
        /* test Env demo */
        test_env();
    }
    while (1)
    {
        /* 按键滤波和检测由后台systick中断服务程序实现，我们只需要调用bsp_GetKey读取键值即可。 */
        ucKeyCode = bsp_GetKey(); /* 读取键值, 无键按下时返回 KEY_NONE = 0 */
        if (ucKeyCode != KEY_NONE)
        {
            switch (ucKeyCode)
            {
            case KEY_DOWN_K1: /* K1键按下 */
                printf("K1键按下\r\n");
                break;

            case KEY_UP_K1: /* K1键弹起 */
                printf("K1键弹起\r\n");
                break;

            case KEY_LONG_K1:
                printf("K1键长按\r\n");
                break;

            default:
                /* 其它的键值不处理 */
                break;
            }
        }
    }
}
